//
//  DataManager.m
//  AssistantDriving
//
//  Created by dcr on 2017/1/10.
//  Copyright © 2017年 邓兆太. All rights reserved.
//

#import "DataManager.h"
#import "FMDatabase.h"
#import "SelectModel.h"
#import "AnswerModel.h"
#import "SectionModel.h"

@implementation DataManager
//根据类型查找数据库
+ (NSArray*)getData:(PracticeModel)type{
    static FMDatabase* dataBase;
    if(dataBase == nil){
        NSString *path = [[NSBundle mainBundle]pathForResource:@"data" ofType:@"sqlite"];
        dataBase = [[FMDatabase alloc] initWithPath:path];
    }
    if(![dataBase open]){
        NSLog(@"open fail");
        return nil;
    }
    NSMutableArray *array = [[NSMutableArray alloc]init];
    switch (type) {
        case chapterPractice:
        {
            NSString *sql = @"select pid, pname, pcount FROM firstlevel";
            FMResultSet *rs = [dataBase executeQuery:sql];
            while([rs next]){
                SelectModel *model = [[SelectModel alloc]init];
                model.pid = [NSString stringWithFormat:@"%d", [rs intForColumn:@"pid"]];
                model.pname = [rs stringForColumn:@"pname"];
                model.pcount = [NSString stringWithFormat:@"%d", [rs intForColumn:@"pcount"]];
                [array addObject:model];
            }
        }
            break;
        case answer:
        {
            NSString *sql = @"select mquestion, mdesc, mid, manswer, mimage, pid, pname, sid, sname, mtype FROM leaflevel";
            FMResultSet *rs = [dataBase executeQuery:sql];
            while([rs next]){
                AnswerModel *model = [[AnswerModel alloc]init];
                model.mquestion = [rs stringForColumn:@"mquestion"];
                model.mdesc = [rs stringForColumn:@"mdesc"];
                model.manswer = [rs stringForColumn:@"manswer"];
                model.mimage = [rs stringForColumn:@"mimage"];
                model.pname = [rs stringForColumn:@"pname"];
                model.sname = [rs stringForColumn:@"sname"];
                model.mid = [NSString stringWithFormat:@"%d", [rs intForColumn:@"mid"]];
                model.pid = [NSString stringWithFormat:@"%d", [rs intForColumn:@"pid"]];
                model.sid = [rs stringForColumn:@"sid"];
                model.mtype = [NSString stringWithFormat:@"%d", [rs intForColumn:@"mtype"]];
                [array addObject:model];
            }
        }
            break;
        case specialPractice:
        {
            NSString *sql = @"select pid, sname, scount, sid, serial FROM secondlevel";
            FMResultSet *rs = [dataBase executeQuery:sql];
            while([rs next]){
                SectionModel *model = [[SectionModel alloc]init];
                model.pid = [NSString stringWithFormat:@"%d", [rs intForColumn:@"pid"]];
                model.sname = [rs stringForColumn:@"sname"];
                model.scount = [NSString stringWithFormat:@"%d", [rs intForColumn:@"scount"]];
                model.sid = [rs stringForColumn:@"sid"];
                model.serial = [NSString stringWithFormat:@"%d", [rs intForColumn:@"serial"]];
                [array addObject:model];
            }

        }
            break;
        default:
            break;
    }
    return array;
}
@end
